package com.ssy.lingxi.search.controller.pc;

import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.search.api.model.request.OftenSelectRequest;
import com.ssy.lingxi.search.api.model.request.SimpleIdRequest;
import com.ssy.lingxi.search.api.model.response.OftenSelectNameResponse;
import com.ssy.lingxi.search.api.model.response.OftenSelectResponse;
import com.ssy.lingxi.search.entity.OftenSelect;
import com.ssy.lingxi.search.service.IOftenSelectService;
import org.modelmapper.TypeToken;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @menu: 商城--常用筛选管理
 * 常用筛选管理
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/9/2
 */
@RestController
@RequestMapping("/search/shop/oftenSelect")
public class OftenSelectController extends BaseController {

    @Resource
    private IOftenSelectService oftenSelectService;

    /**
     * 查询常用筛选信息
     * @param id 常用筛选id
     * @return Wrapper<OftenSelect>
     */
    @RequestMapping(value = "/getOftenSelect", method = RequestMethod.GET)
    public Wrapper<OftenSelectResponse> getOftenSelect(@RequestParam Long id) {
        OftenSelect oftenSelect = oftenSelectService.getOftenSelect(id);
        if(oftenSelect != null){
            return Wrapper.success(this.modelMapper.map(oftenSelect, OftenSelectResponse.class));
        }else{
            return Wrapper.success(null);
        }
    }

    /**
     * 查询常用筛选列表
     * @param pageVO 分页实体
     * @return
     */
    @RequestMapping(value = "/getOftenSelectList", method = RequestMethod.GET)
    public Wrapper<PageData<OftenSelectNameResponse>> getOftenSelectList(HttpServletRequest request, PageVO pageVO) {
        UserLoginCacheDTO sysUser = this.getSysUser(request);
        Page<OftenSelect> result = oftenSelectService.getOftenSelectList(sysUser, pageVO);
        List<OftenSelectNameResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<OftenSelectNameResponse>>(){}.getType());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 新增/修改常用筛选
     * @param oftenSelectRequest 常用筛选实体
     * @return 常用筛选id
     */
    @RequestMapping(value = "/saveOrUpdateOftenSelect", method = RequestMethod.POST)
    public Wrapper<Long> saveOrUpdateOftenSelect(HttpServletRequest request, @RequestBody OftenSelectRequest oftenSelectRequest){
        UserLoginCacheDTO sysUser = this.getSysUser(request);
        return Wrapper.success(oftenSelectService.saveOrUpdateOftenSelect(sysUser, this.modelMapper.map(oftenSelectRequest, OftenSelect.class)));
    }

    /**
     * 删除常用筛选
     * @param simpleIdRequest
     * @return
     */
    @RequestMapping(value = "/deleteOftenSelect", method = RequestMethod.POST)
    public Wrapper<String> deleteOftenSelect(@RequestBody SimpleIdRequest simpleIdRequest){
        return Wrapper.success(oftenSelectService.deleteOftenSelect(simpleIdRequest.getId()));
    }

}
